package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.MessageConstant;
import com.itheima.entity.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfo;

import com.itheima.service.AddressService;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static javax.swing.UIManager.get;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @Reference
    private AddressService addressService;

    @RequestMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderInfo orderInfo){

       /* //判断验证码是否正确
        String validateCode = orderInfo.getValidateCode();
        String telephone = orderInfo.getTelephone();
        Jedis resource = jedisPool.getResource();
        String validateCodeRedis =resource .get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        resource.close();
        if(validateCode !=null && validateCodeRedis != null && validateCode.equals(validateCodeRedis)){*/
           Result result = null;
           try {
               //设置预约类型
               orderInfo.setOrderType(Order.ORDERTYPE_WEIXIN);
               //掉用service层
               result = orderService.submitOrder(orderInfo);
               return result;
           }catch (Exception e){

               e.printStackTrace();
               return new Result(false,MessageConstant.ORDERSETTING_FAIL);
           }
        /*}else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }*/
    }

    //根据id查找三表数据
    @RequestMapping("/findById")
    public Map findById(Integer id){

        Map map = null;
        try {
            map = orderService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    //查询所有address
    @RequestMapping("/findAllAddress")
    public List<Map> findAllAddress(){

        List<Map> list = null;
        try {
            list = addressService.findAllAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

}
